#include "display.h"
#include <stdio.h>

#include "freertos.h"
#include "semphr.h"

#include "AHT21/aht21.h"

#include "module.h"

/*此页面窗口*/
static lv_obj_t * appWindow;
/*背景图片*/
static lv_obj_t * imgBg;

static lv_obj_t * cont_image_bg; 
static lv_obj_t * cont_time; // 时间容器
static lv_obj_t * cont_arc; // 容器

static lv_obj_t * label_date;
static int date_year, date_month, date_day, date_week;
static char week_str[7][4] = {"MON","TUE","WED","THU","FRI","SAT","SUN"}; 
char date_str[11];


static lv_obj_t * label_time;
static int time_hour, time_min, time_sec;
char time_str[6];
char time_sec_str[3];

static lv_obj_t * label_time_sec;

static lv_obj_t * label_week;

static int temp, humi;
char temp_str[3];
char humi_str[3];

static lv_obj_t * label_temp;
static lv_obj_t * label_humi;
static lv_obj_t * icon_temp;
static lv_obj_t * icon_humi;
static lv_obj_t * arc_temp;
static lv_obj_t * arc_humi;

static lv_obj_t * cont_avater; 
static lv_obj_t * icon_avater;


static lv_timer_t * sensor_update_timer;
static lv_timer_t * time_update_timer;

static void date_to_str(int year, int month, int day, char *output)
{
    // 使用格式说明符 %02d 保证两位数显示（自动补零）
    sprintf(output, "%04d/%02d/%02d", year, month, day);
}

void time_to_str(unsigned char hour, unsigned char min, char* output) {
    // 使用格式说明符 %02d 保证两位数显示（自动补零）
    sprintf(output, "%02u:%02u", hour, min);
}

extern SemaphoreHandle_t xSensorSemaphore;
extern QueueHandle_t xSensorDataQueue;
static void sensor_update_timer_cb(lv_timer_t * timer)
{
    aht21_data_t aht21_data;
    if (xSemaphoreTake(xSensorSemaphore, 0) == pdTRUE) {
        if (xQueueReceive(xSensorDataQueue, &aht21_data, 0) == pdTRUE) 
        {
            temp = aht21_data.temperature;
            humi = aht21_data.humidity;
            lv_arc_set_value(arc_temp, temp);
            lv_arc_set_value(arc_humi, humi);
            sprintf(temp_str, "%02d", temp);
            lv_label_set_text(label_temp, temp_str);
            sprintf(humi_str, "%02d", humi);
            lv_label_set_text(label_humi, humi_str);
        }
    }
}

static void time_update_timer_cb(lv_timer_t * timer)
{
    RTC_TimeTypeDef RTC_TimeStruct;
    RTC_DateTypeDef RTC_DateStructure;
    RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
    RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);
    if(time_sec != RTC_TimeStruct.RTC_Seconds) {
        time_to_str(RTC_TimeStruct.RTC_Hours, RTC_TimeStruct.RTC_Minutes, time_str);
        sprintf(time_sec_str, "%02d", RTC_TimeStruct.RTC_Seconds);
        lv_label_set_text(label_time, time_str);
        lv_label_set_text(label_time_sec, time_sec_str);
    }
    if(date_day != RTC_DateStructure.RTC_Date)
    {
        date_year = RTC_DateStructure.RTC_Year + 2000;
        date_month = RTC_DateStructure.RTC_Month;
        date_day = RTC_DateStructure.RTC_Date;
        date_week = RTC_DateStructure.RTC_WeekDay - 1;
        date_to_str(date_year, date_month, date_day, date_str);
        lv_label_set_text(label_date, date_str);
        lv_label_set_text(label_week, week_str[date_week]);
    }

}

/**
  * @brief  创建背景图
  * @param  无
  * @retval 无
  */
static void ImgBg_Create()
{
    // 背景图片
    imgBg = lv_img_create(appWindow);
    lv_img_set_src(imgBg, &image_bg);
    lv_obj_align(imgBg, LV_ALIGN_TOP_LEFT, 0, 0);
    // 背景蒙版
    cont_image_bg = lv_obj_create(appWindow);
    lv_obj_remove_style_all(cont_image_bg);
    lv_obj_set_size(cont_image_bg, 240, 280);
    lv_obj_set_align(cont_image_bg, LV_ALIGN_TOP_LEFT);
    lv_obj_clear_flag(cont_image_bg, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE);     
    lv_obj_set_style_bg_color(cont_image_bg, lv_color_black(), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(cont_image_bg, LV_OPA_40, LV_PART_MAIN); 
    lv_obj_set_style_border_width(cont_image_bg, 0, LV_PART_MAIN);
}



/**
  * @brief  页面初始化事件
  * @param  无
  * @retval 无
  */
static void Setup()
{
    RTC_TimeTypeDef RTC_TimeStruct;
    RTC_DateTypeDef RTC_DateStructure;
    RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
    RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);
    /*将此页面移到前台*/
    lv_obj_move_foreground(appWindow);
    date_year = RTC_DateStructure.RTC_Year + 2000;
    date_month = RTC_DateStructure.RTC_Month;
    date_day = RTC_DateStructure.RTC_Date;
    date_week = RTC_DateStructure.RTC_WeekDay - 1; 
    time_hour = RTC_TimeStruct.RTC_Hours;
    time_min = RTC_TimeStruct.RTC_Minutes;
    time_sec = RTC_TimeStruct.RTC_Seconds;
    temp = 25;
    humi = 80;
    // 背景图片
    // ImgBg_Create();
    /* 时间 */
    // 时间容器 
    cont_time = lv_obj_create(appWindow);
    lv_obj_set_y(cont_time, 30); 
    lv_obj_set_size(cont_time, 190, 130);
    lv_obj_set_align(cont_time, LV_ALIGN_TOP_MID);
    lv_obj_clear_flag(cont_time, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE); 
    lv_obj_set_style_bg_color(cont_time, lv_color_black(), LV_PART_MAIN); 
    lv_obj_set_style_bg_opa(cont_time, LV_OPA_20, LV_PART_MAIN); 
    lv_obj_set_style_border_width(cont_time, 0, LV_PART_MAIN);

    // 年月日 24pix 1234567890/ADEFHIMNORSTUW
    label_date = lv_label_create(cont_time);
    lv_obj_set_width(label_date, LV_SIZE_CONTENT);  
    lv_obj_set_height(label_date, LV_SIZE_CONTENT);  
    date_to_str(date_year, date_month, date_day, date_str);
    lv_label_set_text(label_date, date_str);
    lv_obj_set_style_text_font(label_date, &font_ds_digital_bold_24, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(label_date, lv_color_white(), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(label_date, LV_TEXT_ALIGN_RIGHT, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_align(label_date, LV_ALIGN_TOP_MID, 0); 

    // 时分 72px 1234567890:
    label_time = lv_label_create(cont_time);
    lv_obj_set_width(label_time, LV_SIZE_CONTENT);  
    lv_obj_set_height(label_time, LV_SIZE_CONTENT);  
    time_to_str(time_hour, time_min, time_str);
    lv_label_set_text(label_time, time_str);
    lv_obj_set_style_text_font(label_time, &font_ds_digital_bold_72, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(label_time, lv_color_white(), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(label_time, LV_TEXT_ALIGN_RIGHT, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_align(label_time, LV_ALIGN_CENTER, 0); 

    // 星期 24px 1234567890/ADEFHIMNORSTUW
    label_week = lv_label_create(cont_time);
    lv_obj_set_width(label_week, LV_SIZE_CONTENT);  
    lv_obj_set_height(label_week, LV_SIZE_CONTENT);  
    lv_label_set_text(label_week, week_str[date_week]);
    lv_obj_set_style_text_font(label_week, &font_ds_digital_bold_24, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(label_week, lv_color_white(), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(label_week, LV_TEXT_ALIGN_RIGHT, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_align(label_week, LV_ALIGN_BOTTOM_LEFT, 0); 

    // 秒 24px 1234567890/ADEFHIMNORSTUW
    label_time_sec = lv_label_create(cont_time);
    lv_obj_set_width(label_time_sec, LV_SIZE_CONTENT);  
    lv_obj_set_height(label_time_sec, LV_SIZE_CONTENT);  
    sprintf(time_sec_str, "%02d", time_sec);
    lv_label_set_text(label_time_sec, time_sec_str);
    lv_obj_set_style_text_font(label_time_sec, &font_ds_digital_bold_24, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(label_time_sec, lv_color_white(), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(label_time_sec, LV_TEXT_ALIGN_RIGHT, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_align(label_time_sec, LV_ALIGN_BOTTOM_RIGHT, 0); 


    /* 温湿度 和 头像 */
    cont_arc = lv_obj_create(appWindow);
    lv_obj_set_x(cont_arc, 0);
    lv_obj_set_y(cont_arc, 70); 
    lv_obj_set_size(cont_arc, 210, 80);
    lv_obj_set_align(cont_arc, LV_ALIGN_CENTER);
    lv_obj_clear_flag(cont_arc, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE);  
    lv_obj_set_style_bg_color(cont_arc, lv_color_black(), LV_PART_MAIN); 
    lv_obj_set_style_bg_opa(cont_arc, LV_OPA_20, LV_PART_MAIN); 
    lv_obj_set_style_border_width(cont_arc, 0, LV_PART_MAIN);

    // 头像容器
    cont_avater = lv_btn_create(cont_arc);
    lv_obj_set_x(cont_avater, 0);
    lv_obj_set_y(cont_avater, 0);
    lv_obj_set_width(cont_avater, 48);
    lv_obj_set_height(cont_avater, 48);
    lv_obj_set_align(cont_avater, LV_ALIGN_CENTER);
    lv_obj_add_flag(cont_avater, LV_OBJ_FLAG_SCROLL_ON_FOCUS);    
    lv_obj_clear_flag(cont_avater, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE); 
    lv_obj_set_style_radius(cont_avater, 48, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(cont_avater, lv_color_hex(0xFF8080), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(cont_avater, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    // 头像图标
    icon_avater = lv_img_create(cont_avater);
    lv_img_set_src(icon_avater, &image_icon_avatar);
    lv_obj_set_align(icon_avater, LV_ALIGN_CENTER);

    // 温度圆弧
    arc_temp = lv_arc_create(cont_arc);
    lv_obj_clear_flag(arc_temp, LV_OBJ_FLAG_CLICKABLE);  // 关键代码：禁止点击/拖动
    lv_obj_set_width(arc_temp, 48);
    lv_obj_set_height(arc_temp, 48);
    lv_obj_set_align(arc_temp, LV_ALIGN_LEFT_MID);
    lv_arc_set_value(arc_temp, temp);
    lv_arc_set_range(arc_temp, -20, 60);
    lv_obj_set_style_arc_width(arc_temp, 4, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_arc_color(arc_temp, lv_color_hex(0xff0000), LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_arc_opa(arc_temp, 255, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_arc_width(arc_temp, 4, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(arc_temp, lv_color_hex(0xff0000), LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(arc_temp, 255, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(arc_temp, 0, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(arc_temp, 0, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(arc_temp, 0, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(arc_temp, 0, LV_PART_KNOB | LV_STATE_DEFAULT);
    // 温度数值
    label_temp = lv_label_create(arc_temp);
    lv_obj_set_width(label_temp, LV_SIZE_CONTENT);  
    lv_obj_set_height(label_temp, LV_SIZE_CONTENT);  
    lv_obj_set_align(label_temp, LV_ALIGN_CENTER);
    lv_obj_set_y(label_temp, -2);
    sprintf(temp_str, "%02d", temp);
    lv_label_set_text(label_temp, temp_str);
    lv_obj_set_style_text_font(label_temp, &font_ds_digital_bold_24, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(label_temp, lv_color_hex(0xff0000), LV_PART_MAIN | LV_STATE_DEFAULT);
    // 温度图标
    icon_temp = lv_img_create(arc_temp);
    lv_img_set_src(icon_temp, &image_icon_temp);
    lv_obj_set_align(icon_temp, LV_ALIGN_BOTTOM_MID);
    lv_obj_set_y(icon_temp, 0);

    // 湿度圆弧
    arc_humi = lv_arc_create(cont_arc);
    lv_obj_clear_flag(arc_humi, LV_OBJ_FLAG_CLICKABLE);  // 关键代码：禁止点击/拖动
    lv_obj_set_width(arc_humi, 48);
    lv_obj_set_height(arc_humi, 48);
    lv_obj_set_align(arc_humi, LV_ALIGN_RIGHT_MID);
    lv_arc_set_value(arc_humi, humi); 
    lv_obj_set_style_arc_width(arc_humi, 4, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_arc_color(arc_humi, lv_color_hex(0x1296db), LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_arc_opa(arc_humi, 255, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_arc_width(arc_humi, 4, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(arc_humi, lv_color_hex(0x1296db), LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(arc_humi, 255, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(arc_humi, 0, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(arc_humi, 0, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(arc_humi, 0, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(arc_humi, 0, LV_PART_KNOB | LV_STATE_DEFAULT);
    // 湿度数值
    label_humi = lv_label_create(arc_humi);
    lv_obj_set_width(label_humi, LV_SIZE_CONTENT);  
    lv_obj_set_height(label_humi, LV_SIZE_CONTENT);  
    lv_obj_set_align(label_humi, LV_ALIGN_CENTER);
    lv_obj_set_y(label_humi, -2);
    sprintf(humi_str, "%02d", humi);
    lv_label_set_text(label_humi, humi_str);
    lv_obj_set_style_text_font(label_humi, &font_ds_digital_bold_24, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(label_humi, lv_color_hex(0x1296db), LV_PART_MAIN | LV_STATE_DEFAULT);
    // 湿度图标
    icon_humi = lv_img_create(arc_humi);
    lv_img_set_src(icon_humi, &image_icon_humi);
    lv_obj_set_align(icon_humi, LV_ALIGN_BOTTOM_MID);
    lv_obj_set_y(icon_humi, 0);

    // 事件
    sensor_update_timer = lv_timer_create(sensor_update_timer_cb, 250, NULL);
    time_update_timer = lv_timer_create(time_update_timer_cb, 500, NULL);

}

/**
  * @brief  页面退出事件
  * @param  无
  * @retval 无
  */
static void Exit()
{
    lv_timer_del(sensor_update_timer); // 清除事件
    lv_timer_del(time_update_timer); 
    lv_obj_clean(appWindow);
}

/**
  * @brief  页面事件
  * @param  btn:发出事件的按键
  * @param  event:事件编号
  * @retval 无
  */
static void Event(void* btn, int event)
{
    if(event == PAGE_EVENT_KEY1_SC)
    { 
        // 切换页面
        page_push(PAGE_Menu);
				
    }
}





/**
  * @brief  页面注册
  * @param  pageID:为此页面分配的ID号
  * @retval 无
  */
void page_register_Home(uint8_t pageID)
{
    // /*获取分配给此页面的窗口*/
    appWindow = AppWindow_GetCont(pageID);
    
    /*注册至页面调度器*/
    page_register(pageID, Setup, NULL, Exit, Event);
}








